package com.qiuzhiyi.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;

import com.li.bottom.BottomNavigationItem;
import com.li.bottom.BottomNavigationView;
import com.li.widget.FixedViewPager;
import com.qiuzhiyi.R;
import com.qiuzhiyi.base.QZYBaseActivity;
import com.qiuzhiyi.fragment.AllowanceFragment;
import com.qiuzhiyi.fragment.FindJobFragment;
import com.qiuzhiyi.fragment.MineFragment;
import com.qiuzhiyi.fragment.MsgFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends QZYBaseActivity implements BottomNavigationView.OnTabSelectedListener,Runnable{

    @BindView(R.id.view_pager)
    FixedViewPager mViewPager;
    @BindView(R.id.navigation_view)
    BottomNavigationView mNavigationView;

    private static final int REQUEST_CODE_PERMISSION = 241;
    private List<Fragment> mFragmentList;


    @Override
    protected void initSth() {
        super.initSth();
        mFragmentList=new ArrayList<>();
        mFragmentList.add(FindJobFragment.newInstance());
        mFragmentList.add(AllowanceFragment.newInstance());
        mFragmentList.add(MsgFragment.newInstance());

        mFragmentList.add(MineFragment.newInstance());


        mViewPager.setOffscreenPageLimit(mFragmentList.size());
        mNavigationView.post(this);
        mNavigationView.setOnTabSelectedListener(this);
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }
        });
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED
                || ActivityCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(mActivity, new String[]{
                    Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE,
            }, REQUEST_CODE_PERMISSION);
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onSelected(int pos, BottomNavigationItem item) {
        mViewPager.setCurrentItem(pos,false);
    }

    @Override
    public void onUnSelected(int pos, BottomNavigationItem item) {

    }

    @Override
    public void onReSelected(int pos, BottomNavigationItem item) {

    }

    @Override
    public void run() {
        mNavigationView.getChildAt(0).setSelected(true);
    }
}
